package wowraidmanager.client.wishlist.admin;

import wowraidmanager.client.BaseComposite;
import wowraidmanager.client.service.wishlist.admin.ImportUrlService;
import wowraidmanager.client.service.wishlist.admin.ImportUrlServiceAsync;
import wowraidmanager.client.wishlist.utils.CheckError;
import wowraidmanager.shared.model.wishlist.Droplist;
import wowraidmanager.shared.model.wishlist.RaidbossDroplist;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class Import extends BaseComposite{

    private static ImportUiBinder uiBinder = GWT.create(ImportUiBinder.class);

    protected ImportUrlServiceAsync importUrlService = GWT.create(ImportUrlService.class);

    interface ImportUiBinder extends UiBinder<Widget, Import>{
    }

    RaidbossDroplist raidbossdroplist = null;

    @UiField
    TextBox importUrlwowhead;

    @UiField
    TextBox importUrlwow;

    @UiField
    TextBox raidBossName;

    @UiField
    TextBox raidZone;

    @UiField
    TextBox gameVersion;

    @UiField
    Button importUrlwowheadButton;

    @UiField
    Button importUrlwowButton;

    @UiField
    Button importOKButton;

    @UiField
    Button importCancelButton;

    @UiField
    FlexTable raidbossDropitem;

    public Import(){
        initWidget(uiBinder.createAndBindUi(this));
        setButton();
    }

    private void setButton(){
        importUrlwowheadButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                setImportwowhead();
            }
        });

        importUrlwowButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                setImportwow();
            }
        });


        importOKButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                if(raidbossdroplist != null){
                    errorString = CheckError.checkImportTextBox(raidBossName.getText(), raidZone.getText(), gameVersion.getText());

                    if(errorString.equals("")){
                        importUrlService.setRaidbossDroplist(raidbossdroplist, raidBossName.getText(), raidZone.getText(), gameVersion.getText(), new AsyncCallback<Void>(){

                            public void onSuccess(Void result){
                                setInfoMessage("取り込みに成功しました。");
                                clearErrorMessage();
                            }

                            public void onFailure(Throwable caught){
                                clearInfoMessage();
                                setErrorMessage("取り込みに失敗しました。");
                            }
                        });
                    }else{
                        clearInfoMessage();
                        setErrorMessage(errorString);
                    }
                }else{
                    clearInfoMessage();
                    setErrorMessage("入力データがありません。");
                }
            }
        });

        importCancelButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                setAllclear();
            }
        });

    }


    protected void setraidDropitem(){
        raidBossName.setText(raidbossdroplist.getBossName());

        raidbossDropitem.removeAllRows();
        raidbossDropitem.setBorderWidth(1);
        raidbossDropitem.setText(0, 0, "Normal");
        int row = 1;
        for(Droplist droplist:raidbossdroplist.getDroplist()){
            if(!droplist.isHeroic()){
                Anchor item = new Anchor();
                item.setHTML("<a href=\"http://www.wowhead.com/item=" + droplist.getId() + "\" rel=\"item=" + droplist.getId() + "\" class=\"" + droplist.getItemColor() + "\" target=\"_blank\">[" + droplist.getItemName() + "]<a/>");
                raidbossDropitem.setWidget(row, 0, item);
                row++;
            }
        }

        raidbossDropitem.setText(row, 0, "Heroic");
        row++;

        for(Droplist droplist:raidbossdroplist.getDroplist()){
            if(droplist.isHeroic()){
                Anchor item = new Anchor();
                item.setHTML("<a href=\"http://www.wowhead.com/item=" + droplist.getId() + "\" rel=\"item=" + droplist.getId() + "\" class=\"" + droplist.getItemColor() + "\" target=\"_blank\">[" + droplist.getItemName() + "]<a/>");
                raidbossDropitem.setWidget(row, 0, item);
                row++;
            }
        }
    }

    protected void setImportwowhead(){
        errorString = CheckError.checkImportUrlwowhead(importUrlwowhead.getText());
        if(errorString.equals("")){
            importRaidBosswowhead(importUrlwowhead.getText());
        }else{
            setErrorMessage(errorString);
        }
    }


    protected void setImportwow(){
        errorString = CheckError.checkImportUrlwow(importUrlwow.getText());

        if(errorString.equals("")){
            importRaidBosswow(importUrlwow.getText());
        }else{
            setErrorMessage(errorString);
        }
    }

    private void importRaidBosswow(String importurlwow){
        importUrlService.getImportUrlwow(importurlwow, new AsyncCallback<RaidbossDroplist>(){

            public void onSuccess(RaidbossDroplist result){
                raidbossdroplist = result;
                if(raidbossdroplist != null){
                    setraidDropitem();
                }
                clearErrorMessage();
            }

            public void onFailure(Throwable caught){
                clearInfoMessage();
                setErrorMessage("取り込みに失敗しました。再度実行してください。");
            }
        });
    }

    private void importRaidBosswowhead(String importUrl){
        importUrlService.getImportUrlwowhead(importUrl, new AsyncCallback<RaidbossDroplist>(){

            public void onSuccess(RaidbossDroplist result){
                raidbossdroplist = result;
                if(raidbossdroplist != null){
                    setraidDropitem();
                }
                clearErrorMessage();
            }

            public void onFailure(Throwable caught){
                clearInfoMessage();
                setErrorMessage("取り込みに失敗しました。再度実行してください。");
            }
        });

    }

    protected void setAllclear(){
        importUrlwowhead.setText("");
        raidBossName.setText("");
        raidZone.setText("");
        gameVersion.setText("");
        raidbossDropitem.removeAllRows();
        raidbossdroplist = null;
    }

}
